package DB;

import Model.*;

import java.sql.*;
import java.util.ArrayList;


public class DBVaregenre {

	private Connection con;
	private DBVaregenre_Vare dbVgv;
	
	private static DBVaregenre instance = null;
    
    public static DBVaregenre getInstance()
    {
        if (instance == null)
        {
          instance = new DBVaregenre();
        }
        return instance;
    }
    
	public DBVaregenre()
	{
		con = DBConnection.getInstance().getDBcon();
		dbVgv = DBVaregenre_Vare.getInstance();
	}
	
	private void create(String wClause)
	{
	    String query="INSERT INTO Varegenre ";
	    
		if (wClause.length()>0)
		{
			query=query+ wClause;
			
			Statement stmt;
			
			try
			{
				stmt = con.createStatement(); 
				stmt.executeUpdate(query); 
				con.commit(); 
				stmt.close();
			}
			catch (Exception e)
			{
				e.printStackTrace(); 
			}
		}
		else
		{
		}
	}
	
	public void createSalg(Varegenre vg)
	{
		String wClause = "VALUES " + "" + vg.getGenre() + ")";
        create(wClause);
	}
	
	public void updateVaregenre(Varegenre vg)
	{
		PreparedStatement prepstmt;
		try 
		{     
		  prepstmt = con.prepareStatement
		  	("UPDATE Varegenre SET " 
			   	+ "dato = '" + vg.getGenre() + "',"
			   	+ " WHERE varegenreid = " + vg.getVaregenreid() + "");
		  prepstmt.executeUpdate();
		             
		  con.commit();
		  prepstmt.close();
		}
		catch (Exception e) 
		{
		  e.printStackTrace();
		}
	}
	
	public void deleteVaregenre(int vgid)
	{
		PreparedStatement prepstmt;
		try 
		{     
		  prepstmt = con.prepareStatement
		    ("DELETE FROM Varegenre WHERE usid = " + vgid + "");   
		  prepstmt.executeUpdate();          
		  con.commit();
		  prepstmt.close();
		}
		catch (Exception e) 
		{
		  e.printStackTrace();
		}
	}
	
	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM Varegenre";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	private Varegenre singleWhere(String wClause, boolean retrieveAssocation)
	{
		ResultSet results;
		Varegenre vg = new Varegenre();
	    String query =  buildQuery(wClause);
		try{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if(results.next())
	 		{
	 			vg = buildVaregenre(results,retrieveAssocation);
                stmt.close();
			}
		}//end try	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
	 	
		return vg;
	}
	
	public ArrayList<Varegenre> getAlleVaregenre()
	{
		return miscWhere("");
	}
	
	private ArrayList<Varegenre> miscWhere(String wClause)
	{
		ResultSet results;
		ArrayList list = new ArrayList();	
		
		String query =  buildQuery(wClause);
  
  		try
  		{ // read the customer from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);		 		

			while( results.next() )
			{
				Varegenre vgObj = new Varegenre();
		     	 vgObj = buildVaregenre(results, true);	
                 list.add(vgObj);	
			}//end while
			stmt.close();

		}//slut try	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	private Varegenre buildVaregenre(ResultSet results, boolean retrieveAssocation)
    {   
		Varegenre vg = new Varegenre();
          try
          {
            	vg.setVaregenreid(results.getInt(1));
                vg.setGenre(results.getString(2));
          }
         catch(Exception e)
         {
         }
         return vg;
    }
    
	public Varegenre findVaregenre(int id, boolean retrieveAssocation) 
    {
    	String wClause = "  varegenreid = " + Integer.toString(id) + "";
    	return singleWhere(wClause, retrieveAssocation);
    }
	


	public Varegenre findVaregenreNavn(String navn, boolean retrieveAssocation) {
    	String wClause = "  genre = '" + navn + "'";
    	return singleWhere(wClause, retrieveAssocation);
	}

}
